#include "SocketHeader.hpp"
#include <iostream>

using namespace std;

/**
 * 这是嵌入式端的代码
 */

int main(void)
{
    /* 创建套接字接口 */
    int s_stream,s_cmd;

    s_stream = socket(AF_INET, SOCK_STREAM, 0);  // 流数据
    s_cmd = socket(AF_INET, SOCK_STREAM, 0);    // 命令
    if(s_stream < 0 || s_cmd < 0)
    {
        std::cerr<< "socket create error"<<std::endl;
        return -1;
    }
    std::cout<<"socket create success"<<std::endl;

    /* 返回值暂存 */
    int res_stream,res_cmd;

    /* 套接字接口配置 */
    struct sockaddr_in sockAddrConfig;

    sockAddrConfig.sin_family = AF_INET;
    sockAddrConfig.sin_port = htons(8010);
    sockAddrConfig.sin_addr.s_addr = inet_addr("119.28.6.20");

    res_stream = connect(s_stream, (struct sockaddr*)&sockAddrConfig,sizeof(sockAddrConfig));
    if(res_stream)
    {
        std::cerr<<"socket(Stream) connect error"<<std::endl;
        return -2;
    }

    sockAddrConfig.sin_port = htons(8012);
    res_cmd = connect(s_cmd, (struct sockaddr*)&sockAddrConfig,sizeof(sockAddrConfig));
    if(res_cmd)
    {
        std::cerr<<"socket(cmd) connect error"<<std::endl;
        return -2;
    }
    std::cout<<"socket connect success"<<std::endl
        <<"begin verify"<<std::endl;
    char buffer_cmd[4] = "Emb";
    res_cmd = send(s_cmd,buffer_cmd,4,0);
    std::cout<<"send cmd:"<<buffer_cmd<<std::endl;

    /**
     * 连接建立完成
     * 开始传输数据
     */
    std::cout<<"communication establish"<<std::endl
        <<"begin transmit"<<std::endl;
    
    
    for(int co = 0;;)   // 循环接收
    {
        res_stream = recv(s_stream,&co,sizeof(co),0);
        if(res_stream < 0)
        {
            std::cerr<<"recv error"<<std::endl;
            return -1;
        }
        std::cout<<"recv data:"<<co<<std::endl;
    }
    return 0;
}